package hichecker.tools.logging;

import java.io.IOException;
import java.io.PrintStream;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class CustomLogger {
	private static Logger logger = Logger.getRootLogger();

	public static void setup() throws IOException {
		// redirect system.err
		System.setErr(new PrintStream(System.err) {
            @Override
			public void print(final String string) {
                logger.error(string);
            }
		});
		
		// message layout
		PatternLayout layout = new PatternLayout("%d{ISO8601} %-5p [%t] %c: %m%n");
		
		// console appender
		ConsoleAppender errorConsoleAppender = new ConsoleAppender(layout);
		logger.addAppender(errorConsoleAppender);

		// file appender, clear daily
		DailyRollingFileAppender fileAppender = new DailyRollingFileAppender(layout,
				"logs/HiChecker.log", "'.'yyyy-MM-dd");
		logger.addAppender(fileAppender);

		// set logging level
		logger.setLevel(Level.DEBUG);
    }

}
